<?php
    class ApartmentModel{
        var $apartment_old;
        var $apartment_id;
        var $apartment_name;
        var $abbreviation_name;
        var $floor_number;
        var $number_of_rooms;
        var $beds;
        var $property;
        var $remarks;
        var $add_time;
        var $status;
        var $number_len;
    }
    class Floor{
        var $apartment_id;
        var $floor;
        var $property;
        var $number_of_rooms;
        var $remarks;
        var $status;
    }
    class Dormitory{
        var $apartment_id;
        var $floor;
        var $property;
        var $dormitory_id;
        var $beds;
        var $remarks;
        var $status;
    }
    class ApartmentDAL{

        public function getApartmentList($action,$page,$pageSize,$property,$search=''){
            $result=null;

            //1 查询全部
            $query_all="select apartment_id,apartment_name,floor_number as floor,abbreviation_name,property,remarks 
            from tb_apartment_info where status=:status 
            order by tb_apartment_info.apartment_id LIMIT :page,:pageSize;";

            //2 查询全部，带统计条件,按公寓编号或名称查询
            $query_all_search="select apartment_id,apartment_name,floor_number as floor,abbreviation_name,property,remarks 
            from tb_apartment_info 
            where status=:status 
            and (apartment_id=:search_id or apartment_name=:search_name) 
            order by tb_apartment_info.apartment_id LIMIT :page,:pageSize;";

            //3 按宿舍分类查询，不带查询条件
            $query_property="select apartment_id,apartment_name,floor_number as floor,abbreviation_name,property,remarks 
            from tb_apartment_info where status=:status and property=:property 
            order by tb_apartment_info.apartment_id LIMIT :page,:pageSize;";

            //4 按宿舍分类查询，带查询条件,按公寓编号或名称查询
            $query_property_search="select apartment_id,apartment_name,floor_number as floor,abbreviation_name,property,remarks 
            from tb_apartment_info 
            where status=:status 
            and property=:property 
            and (apartment_id=:search_id or apartment_name=:search_name) 
            order by tb_apartment_info.apartment_id LIMIT :page,:pageSize;";
            
            $pdo=myPDO::getInstance();
            switch($action){
                case 1:
                    $statement=$pdo->prepare($query_all,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                    $statement->bindValue(":status",1);
                    $statement->bindValue(':page',$page,PDO::PARAM_INT);
                    $statement->bindValue(':pageSize',$pageSize,PDO::PARAM_INT);
                    break;
                case 2:
                    $statement=$pdo->prepare($query_all_search,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                    $statement->bindValue(":status",1);
                    $statement->bindValue(":search_id",$search);
                    $statement->bindValue(":search_name",$search);
                    $statement->bindValue(':page',$page,PDO::PARAM_INT);
                    $statement->bindValue(':pageSize',$pageSize,PDO::PARAM_INT);
                    break;
                case 3:
                    $statement=$pdo->prepare($query_property,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                    $statement->bindValue(":status",1);
                    $statement->bindValue(":property",$property);
                    $statement->bindValue(':page',$page,PDO::PARAM_INT);
                    $statement->bindValue(':pageSize',$pageSize,PDO::PARAM_INT);
                    break;
                case 4:
                    $statement=$pdo->prepare($query_property_search,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                    $statement->bindValue(":status",1);
                    $statement->bindValue(":property",$property);
                    $statement->bindValue(":search_id",$search);
                    $statement->bindValue(":search_name",$search);
                    $statement->bindValue(':page',$page,PDO::PARAM_INT);
                    $statement->bindValue(':pageSize',$pageSize,PDO::PARAM_INT);
                    break;
                default:break;
            }

            if($statement->execute()){
                $result=$statement->fetchALL(PDO::FETCH_ASSOC);
            }
            $statement->closeCursor();

            return $result;
        }
        //获取公寓数量
        public function getApartmentCount($action,$property,$search=''){
            //1 查询全部
            $query_all="select count(apartment_id) as count 
            from tb_apartment_info where status=:status;";

            //2 查询全部，带统计条件,按公寓编号或名称查询
            $query_all_search="select count(apartment_id) as count 
            from tb_apartment_info 
            where status=:status 
            and (apartment_id=:search or apartment_name=:search);";

            //3 按宿舍分类查询，不带查询条件
            $query_property="select count(apartment_id) as count 
            from tb_apartment_info where status=:status and property=:property;";

            //4 按宿舍分类查询，带查询条件,按公寓编号或名称查询
            $query_property_search="select count(apartment_id) as count 
            from tb_apartment_info 
            where status=:status 
            and property=:property 
            and (apartment_id=:search or apartment_name=:search);";

            $pdo=myPDO::getInstance();
            switch($action){
                case 1:
                    $statement=$pdo->prepare($query_all,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                    $statement->bindValue(":status",1);
                    break;
                case 2:
                    $statement=$pdo->prepare($query_all_search,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                    $statement->bindValue(":status",1);
                    $statement->bindValue(":search",$search);
                    break;
                case 3:
                    $statement=$pdo->prepare($query_property,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                    $statement->bindValue(":status",1);
                    $statement->bindValue(":property",$property);
                    break;
                case 4:
                    $statement=$pdo->prepare($query_property_search,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                    $statement->bindValue(":status",1);
                    $statement->bindValue(":property",$property);
                    $statement->bindValue(":search",$search);
                    break;
                default:break;
            }

            if($statement->execute()){
                $result=$statement->fetch(PDO::FETCH_ASSOC);
            }
            $statement->closeCursor();

            return $result;
        }

        //获取不同状态宿舍的数量
        public function getApartmentDormitoryNumber($action,$status,$apartmentID=-1,$floor=0){
            $result=null;
            /**
             * $action
             * 1-查询公寓所有楼层，精确匹配
             * 2-查询公寓所有楼层，范围匹配
             * 3-查询公寓单独楼层，精确匹配
             * 4-查询公寓单独楼层，范围匹配
             */

            //查询停用的宿舍 status=0
            //查询公寓未分配的宿舍 status=1
            //查询公寓已分配给辅导员但未分配给学生的宿舍 status=2
            //查询公寓已分配给学生的宿舍 status=3
            $query="select count(*) as count from tb_dormitory_info 
            where apartment_id=:apartment_id and status=:status";

            //查询公寓已分配的宿舍 status>1
            $query_range="select count(*) as count from tb_dormitory_info 
            where apartment_id=:apartment_id and status>:status";

            //按楼层查询停用的宿舍 status=0
            //按楼层查询公寓未分配的宿舍 status=1
            //按楼层查询公寓已分配给辅导员但未分配给学生的宿舍 status=2
            //按楼层查询公寓已分配给学生的宿舍 status=3
            $query_floor="select count(*) as count from tb_dormitory_info 
            where apartment_id=:apartment_id and floor=:floor and status=:status";

            //8 按楼层查询公寓已分配的宿舍 status>1
            $query_floor_Range="select count(*) as count from tb_dormitory_info 
            where apartment_id=:apartment_id and floor=:floor and status>:status";


            $pdo=myPDO::getInstance();
            switch($action){
                case 1:
                    $statement=$pdo->prepare($query,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                    $statement->bindValue(":apartment_id",$apartmentID);
                    $statement->bindValue(":status",$status);
                    break;
                case 2:
                    $statement=$pdo->prepare($query_range,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                    $statement->bindValue(":apartment_id",$apartmentID);
                    $statement->bindValue(":status",$status);
                    break;
                case 3:
                $statement=$pdo->prepare($query_floor,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                    $statement->bindValue(":apartment_id",$apartmentID);
                    $statement->bindValue(":floor",$floor);
                    $statement->bindValue(":status",$status);
                    break;
                case 4:
                    $statement=$pdo->prepare($query_floor_Range,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                    $statement->bindValue(":apartment_id",$apartmentID);
                    $statement->bindValue(":floor",$floor);
                    $statement->bindValue(":status",$status);
                    break;

                default:break;
                
            }

            if($statement->execute()){
                $result=$statement->fetch(PDO::FETCH_ASSOC);
            }

            return $result;
        }
        //获取公寓宿舍数量,查询公寓所有层数的房间数
        public function getApartmentDormitoryCount($apartmentID){
            $result=null;
            $query="select number_of_rooms as count from tb_floor_info where apartment_id=:apartment_id";

            $pdo=myPDO::getInstance();
            $statement=$pdo->prepare($query,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
            $statement->bindValue(":apartment_id",$apartmentID);
            if($statement->execute()){
                $result=$statement->fetchAll(PDO::FETCH_ASSOC);
            }

            $statement->closeCursor();
            //$pdo=null;
            return $result;
        }


        //获取每层宿舍数量
        public function getFloorDormitoryCount($apartmentID,$floor){
            $row=null;
            $query="select number_of_rooms as count from tb_floor_info 
            where apartment_id=:apartment_id and floor=:floor;";

            $pdo=myPDO::getInstance();
            $statement=$pdo->prepare($query,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
            $statement->bindValue(":apartment_id",$apartmentID);
            $statement->bindValue(":floor",$floor);
            if($statement->execute()){
                $row=$statement->fetch(PDO::FETCH_ASSOC);
            }

            $statement->closeCursor();
            //$pdo=null;
            return $row;
        }

        //获取公寓最大的编号
        public function getApartmentIDMax(){
            $query="select max(apartment_id) as max from tb_apartment_info;";
            $pdo=$pdo=myPDO::getInstance();
            $row=$pdo->query($query);
            $result=$row->fetch(PDO::FETCH_ASSOC);
            $row->closeCursor();

            return $result;
        }

        //添加公寓
        public function addApartment($apartment){
            $executeReturn=false;
            //插入信息

            //获取数据库连接
            $pdo=myPDO::getInstance();
            try{
                $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
                $pdo->beginTransaction();

                $sql_apartment = "insert into 
                tb_apartment_info(apartment_id,apartment_name,abbreviation_name,property,floor_number,remarks,status,add_time) 
                values(:apartment_id,:apartment_name,:abbreviation_name,:property,:floor_number,:remarks,1,now())";

                $statement=$pdo->prepare($sql_apartment,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                $statement->bindValue(":apartment_id",$apartment->apartment_id);
                $statement->bindValue(":apartment_name",$apartment->apartment_name);
                $statement->bindValue(":abbreviation_name",$apartment->abbreviation_name);
                $statement->bindValue(":property",$apartment->property);
                $statement->bindValue(":floor_number",$apartment->floor_number);
                $statement->bindValue(":remarks",$apartment->remarks);
                $statement->execute();

                $sql_floor = "insert into 
                tb_floor_info(apartment_id,floor,property,number_of_rooms,beds,status) 
                values(:apartment_id,:floor,:property,:number_of_rooms,:beds,1)";

                $executeReturn=false;
                $statement=$pdo->prepare($sql_floor,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                for($i=1;$i<=intval($apartment->floor_number);$i++){
                    $statement->bindValue(":apartment_id",$apartment->apartment_id);
                    $statement->bindValue(":floor",$i);
                    $statement->bindValue(":property",$apartment->property);
                    $statement->bindValue(":number_of_rooms",$apartment->number_of_rooms);
                    $statement->bindValue(":beds",$apartment->beds);
                    //$value=array(':apartment_id'=>$apartment->apartment_id,':floor'=>$i,':property'=>$apartment->property,':number_of_rooms'=>$apartment->number_of_rooms);
                    $executeReturn=$statement->execute();
                }

                $sql_dormitory = "insert into 
                tb_dormitory_info(apartment_id,floor,property,dormitory_id,beds,status) 
                values(:apartment_id,:floor,:property,:dormitory_id,:beds,1)";

                $executeReturn=false;
                $statement=$pdo->prepare($sql_dormitory,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                for($i=1;$i<=intval($apartment->floor_number);$i++){
                    $floor_id=$apartment->number_len*$i;
                    for($j=1;$j<=intval($apartment->number_of_rooms);$j++){
                        $statement->bindValue(":apartment_id",$apartment->apartment_id);
                        $statement->bindValue(":floor",$i);
                        $statement->bindValue(":property",$apartment->property);
                        $statement->bindValue(":dormitory_id",$floor_id+$j);
                        $statement->bindValue(":beds",$apartment->beds);
                        //array(':apartment_id'=>$apartment->apartment_id,':floor'=>$i,':property'=>$apartment->property,':dormitory_id'=>$apartment->number_len+j,':beds'=>$apartment->beds);
                        $executeReturn=$statement->execute();
                    }
                }
                    
                if($executeReturn){
                    $pdo->commit();
                }
            }catch(Exception $e){
                $pdo->rollBack();
                //echo "Failed: ".$e->getMessage();
            }
            
            //$statement->rowCount();
            return $executeReturn;
        }

        //更新公寓信息
        public function updateApartmentInformation($apartmentObject,$apartmentObjectOld){
            $executeReturn=false;
            $apartment=$apartmentObject->apartment;
            $floors=$apartmentObject->floor;

            $floorOld=$apartmentObjectOld->floor;

            //获取数据库连接
            $pdo=myPDO::getInstance();
            try{
                $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
                $pdo->beginTransaction();

                //$floorOld=$apartmentObjectOld->floor;
                
                //更新公寓信息
                $update_apartment="update tb_apartment_info set apartment_name=:apartment_name,abbreviation_name=:abbreviation_name,
                property=:property,remarks=:remarks,status=:status where apartment_id=:apartment_id;";

                $statement=$pdo->prepare($update_apartment,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                $statement->bindValue(":apartment_name",$apartment->name);
                $statement->bindValue(":abbreviation_name",$apartment->abbreviation);
                $statement->bindValue(":property",$apartment->property);
                //$statement->bindValue(":floor_number",$apartment->floor);
                $statement->bindValue(":remarks",$apartment->remarks);
                $statement->bindValue(":status",$apartment->status);
                $statement->bindValue(":apartment_id",$apartment->id);
                $statement->execute();

                //更新楼层信息
                $update_floor="update tb_floor_info set property=:property,number_of_rooms=:number_of_rooms,beds=:beds,
                remarks=:remarks,status=:status where apartment_id = :apartment_id and floor = :floor;";
                $statement=$pdo->prepare($update_floor,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                foreach($floors as $floor){
                    $statement->bindValue(":property",$floor->property);
                    $statement->bindValue(":number_of_rooms",$floor->rooms);
                    $statement->bindValue(":beds",$floor->beds);
                    $statement->bindValue(":remarks",$floor->remarks);
                    $statement->bindValue(":status",$floor->status);
                    $statement->bindValue(":apartment_id",$apartment->id);
                    $statement->bindValue(":floor",$floor->floor);
                    //$value=array(':apartment_id'=>$apartment->apartment_id,':floor'=>$i,':property'=>$apartment->property,':number_of_rooms'=>$apartment->number_of_rooms);
                    $executeReturn=$statement->execute();
                }

                //更新每一层宿舍的参数
                $count=0;
                foreach($floors as $floor){
                    $c=0;
                    $number_len=$floor->rooms;
                    while($number_len>1){
                        ++$c;
                        $number_len/=10;
                    }
                    if($c<3){
                        $number_len=100;
                    }else{
                        $number_len=pow(10,$c);
                    }
                    $floor_id=$number_len * $floor->floor;

                    //更新房间数
                    if($floor->rooms > $floorOld[$count]->rooms){
                        $query_dormitory="insert into tb_dormitory_info(apartment_id,floor,property,dormitory_id,beds,status) 
                        values(:apartment_id,:floor,:property,:dormitory_id,:beds,:status);";

                        $statement=$pdo->prepare($query_dormitory,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                        for($i=$floorOld[$count]->rooms; $i<$floor->rooms ;$i++){
                            $statement->bindValue(":apartment_id",$apartment->id);
                            $statement->bindValue(":floor",$floor->floor);
                            $statement->bindValue(":property",$floor->property);
                            $statement->bindValue(":dormitory_id",$floor_id+$i+1);
                            $statement->bindValue(":beds",$floor->beds);
                            $statement->bindValue(":status",$floor->status);
                            $executeReturn=$statement->execute();
                        }
                        
                    }else if($floor->rooms < $floorOld[$count]->rooms){
                        $query_delete="delete from tb_dormitory_info where apartment_id=:apartment_id and floor=:floor and dormitory_id>:dormitory_id;";
                        $statement=$pdo->prepare($query_delete,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                        $statement->bindValue(":apartment_id",$apartment->id);
                        $statement->bindValue(":floor",$floor->floor);
                        $statement->bindValue(":dormitory_id",$floor_id+$floor->rooms);
                        $statement->execute();
                    }

                    //更新宿舍类别
                    if($floor->property!=$floorOld[$count]->property){
                        $query_dormitory_property="update tb_dormitory_info set property=:property where apartment_id=:apartment_id and floor=:floor;";
                        $statement=$pdo->prepare($query_dormitory_property,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                        $statement->bindValue(":property",$floor->property);
                        $statement->bindValue(":apartment_id",$apartment->id);
                        $statement->bindValue(":floor",$floor->floor);
                        $statement->execute();

                    }
                    //更新床位数
                    if($floor->beds!=$floorOld[$count]->beds){
                        $query_dormitory_beds="update tb_dormitory_info set beds=:beds where apartment_id=:apartment_id and floor=:floor;";
                        $statement=$pdo->prepare($query_dormitory_beds,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                        $statement->bindValue(":beds",$floor->beds);
                        $statement->bindValue(":apartment_id",$apartment->id);
                        $statement->bindValue(":floor",$floor->floor);
                        $statement->execute();
                    }
                    //更新使用状态
                    if($floor->status!=$floorOld[$count]->status){
                        $query_dormitory_other="update tb_dormitory_info set status=:status where apartment_id=:apartment_id and floor=:floor;";
                        $statement=$pdo->prepare($query_dormitory_other,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                        $statement->bindValue(":status",$floor->status);
                        $statement->bindValue(":apartment_id",$apartment->id);
                        $statement->bindValue(":floor",$floor->floor);
                        $statement->execute();
                    }
                    $count++;
                }

                if($executeReturn){
                    $pdo->commit();
                }
            }catch(Exception $e){
                $pdo->rollBack();
                //echo "Failed: ".$e->getMessage();
            }

            return $executeReturn;
        }
        /*else if($floor->number_of_rooms==$floorOld[$count]->number_of_rooms){
            $query_dormitory="update tb_dormitory_info set property=:property,beds=:beds,status=:status where apartment_id=:apartment_id and floor=:floor";
            $statement=$pdo->prepare($query_dormitory,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
            $statement->bindValue(":property",$floor->property);
            $statement->bindValue(":beds",$floor->beds);
            $statement->bindValue(":status",$floor->status);
            $statement->bindValue(":apartment_id",$apartment->apartment_id);
            $statement->bindValue(":floor",$floor);
            $executeReturn=$statement->execute();
        }
         */
        //
        public function addApartmentFloor(){

        }

        //检查值是否已存在
        public function checkValue($tabelName,$fields){
            $array=array();
            $pdo=myPDO::getInstance();
            foreach($fields as $key=>$value){
                $query="select ".$key." from ".$tabelName." where ".$key."=:".$key;
                $statement=$pdo->prepare($query,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                $statement->bindValue(":".$key,$value);
                $statement->execute();
                if($statement->fetch(PDO::FETCH_ASSOC)){
                    $array[]=$key;
                }
            }
            $statement->closeCursor();
            //$pdo=null;
            return $array;
        }

        //获取公寓信息
        public function getApartmentInformation($apartmentID){
            $row=null;
            $query="select apartment_id as id,apartment_name as name,abbreviation_name as abbreviation,
            floor_number as floor,property,remarks,status 
            from tb_apartment_info where apartment_id=:apartment_id";

            $pdo=myPDO::getInstance();
            $statement=$pdo->prepare($query,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
            $statement->bindValue(":apartment_id",$apartmentID);
            if($statement->execute()){
                $row=$statement->fetch(PDO::FETCH_ASSOC);
            }
            $statement->closeCursor();
            //$pdo=null;
            return $row;
        }
        //获取楼层信息
        public function getApartmentFloorInformation($apartmentID,$floor=0){
            $row=null;
            $queryAll="select apartment_id as id,floor,property,number_of_rooms as rooms,beds,remarks,status 
            from tb_floor_info where apartment_id=:apartment_id";

            $query="select apartment_id as id,floor,property,number_of_rooms as rooms,beds,remarks,status 
            from tb_floor_info where apartment_id=:apartment_id and floor=:floor";
            $pdo=myPDO::getInstance();
            switch($floor){
                case 0:
                $statement=$pdo->prepare($queryAll,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                $statement->bindValue(":apartment_id",$apartmentID);
                if($statement->execute()){
                    $row=$statement->fetchAll(PDO::FETCH_ASSOC);
                }
                break;

                default:
                $statement=$pdo->prepare($query,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                $statement->bindValue(":apartment_id",$apartmentID);
                $statement->bindValue(":floor",$floor);
                if($statement->execute()){
                    $row=$statement->fetch(PDO::FETCH_ASSOC);
                }
                break;
            }
            $statement->closeCursor();
            //$pdo=null;
            return $row;
        }
        //查找宿舍宿舍
        public function selectDormitory($apartmentID,$floor=0,$dormitoryID=0){
            $row=null;
            $query="select apartment_id as id,floor,property,dormitory_id,beds,remarks,status 
            from tb_dormitory_info where apartment_id=:apartment_id and floor=:floor and dormitory_id=:dormitory_id";

            $queryFloor="select apartment_id as id,floor,property,dormitory_id,beds,remarks,status 
            from tb_dormitory_info where apartment_id=:apartment_id and floor=:floor";

            $queryAll="select apartment_id as id,floor,property,dormitory_id,beds,remarks,status 
            from tb_dormitory_info where apartment_id=:apartment_id";

            $pdo=myPDO::getInstance();
            /*if($dormitoryID==0 && $floor==0){
                $statement=$pdo->prepare($queryAll,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                $statement->bindValue(":apartment_id",$apartmentID);
                if($statement->execute()){
                    $row=$statement->fetchAll(PDO::FETCH_ASSOC);
                }
            }else if($dormitoryID==0 && $floor!=0){
                $statement=$pdo->prepare($queryFloor,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                $statement->bindValue(":apartment_id",$apartmentID);
                $statement->bindValue(":floor",$floor);
                if($statement->execute()){
                    $row=$statement->fetchAll(PDO::FETCH_ASSOC);
                }
            }else{
                $statement=$pdo->prepare($query,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                $statement->bindValue(":apartment_id",$apartmentID);
                $statement->bindValue(":floor",$floor);
                $statement->bindValue(":dormitory_id",$dormitoryID);
                if($statement->execute()){
                    $row=$statement->fetch(PDO::FETCH_ASSOC);
                }
            }*/
            $statement=$pdo->prepare($query,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
            $statement->bindValue(":apartment_id",$apartmentID);
            $statement->bindValue(":floor",$floor);
            $statement->bindValue(":dormitory_id",$dormitoryID);
            if($statement->execute()){
                $row=$statement->fetchAll(PDO::FETCH_ASSOC);
            }

            $statement->closeCursor();
            //$pdo=null;
            return $row;
        }
        
        //获取宿舍列表（分页）
        public function getDormitoryList($apartmentID,$floor,$page=0,$pageSize=0){
            $row=null;

            $queryFloor="select apartment_id as id,floor,property,dormitory_id,beds,remarks,status 
            from tb_dormitory_info where apartment_id=:apartment_id and floor=:floor";

            $query_page="select apartment_id as id,floor,property,dormitory_id,beds,remarks,status 
            from tb_dormitory_info where apartment_id=:apartment_id and floor=:floor order by dormitory_id asc limit :page,:page_size;";
            
            $pdo=myPDO::getInstance();

            if($pageSize==0){
                $statement=$pdo->prepare($queryFloor,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                $statement->bindValue(":apartment_id",$apartmentID);
                $statement->bindValue(":floor",$floor);
                if($statement->execute()){
                    $row=$statement->fetchAll(PDO::FETCH_ASSOC);
                }
            }else{
                $statement=$pdo->prepare($query_page,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                $statement->bindValue(":apartment_id",$apartmentID);
                $statement->bindValue(":floor",$floor);
                $statement->bindValue(":page",$page,PDO::PARAM_INT);
                $statement->bindValue(":page_size",$pageSize,PDO::PARAM_INT);
                if($statement->execute()){
                    $row=$statement->fetchAll(PDO::FETCH_ASSOC);
                }
            }

            $statement->closeCursor();
            //$pdo=null;
            return $row;
        }

        //修改宿舍设置
        public function modifyDormitoryStting($apartment,$dormitories){
            $executeReturn=false;
            $pdo=myPDO::getInstance();
            try{
                $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
                $pdo->beginTransaction();
                
                $query="update tb_dormitory_info set beds=:beds,property=:property,status=:status,remarks=:remarks 
                where apartment_id=:apartment_id and dormitory_id=:dormitory_id";

                $statement=$pdo->prepare($query,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                foreach($dormitories as $dormitory){
                    $statement->bindValue(":beds",$dormitory->beds);
                    $statement->bindValue(":property",$dormitory->property);
                    $statement->bindValue(":status",$dormitory->status);
                    $statement->bindValue(":remarks",$dormitory->remarks);
                    $statement->bindValue(":apartment_id",$apartment->id);
                    $statement->bindValue(":dormitory_id",$dormitory->id);
                    $executeReturn=$statement->execute();
                }

                $pdo->commit();
            }catch(Exception $e){
                $pdo->rollBack();
                //$executeReturn="Failed: ".$e->getMessage();
            }

            return $executeReturn;
        }

        //测试用
        public function betaFunction(){
            $executeReturn="";
            $sql_apartment = "insert into tb_account(uid,password,register_time,level,status) values(:uid,:password,now(),'1','1')";
            //获取数据库连接
            
            try{
                $pdo=myPDO::getInstance();
                //插入信息
                
                $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);

                $pdo->beginTransaction();
                
                $statement=$pdo->prepare($sql_apartment,array(PDO::ATTR_CURSOR =>PDO::CURSOR_FWDONLY));
                $statement->bindValue(":uid",'20141305');
                $statement->bindvalue(":password",'25d55ad283aa400af464c76d713c07ad');
                $statement->execute();

                /*$statement=$pdo->prepare($sql_floor,array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
                for($i=1;$i<=$apartmentuid->floor_number;$i++){
                    $statement->bindParam(":apartment_id",$apartment->apartment_id);
                    $statement->bindParam(":floor",$i);
                    $statement->bindParam(":property",$apartment->property);
                    $statement->bindParam(":number_of_rooms",$apartment->number_of_rooms);
                    $executeReturn=$statement->execute();
                }

                $statement=$pdo->prepare($sql_dormitory,array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
                for($i=1;$i<=$apartmentuid->floor_number;$i++){
                    for($j=1;$j<=$apartment->number_of_rooms;$j++){
                        $statement->bindParam(":apartment_id",$apartment->apartment_id);
                        $statement->bindParam(":floor",$i);
                        $statement->bindParam(":property",$apartment->property);
                        $statement->bindParam(":dormitory_id",$apartment->number_len+j);
                        $statement->bindParam(":beds",$apartment->beds);
                        $executeReturn=$statement->execute();
                    }
                }*/
                $pdo->commit();
            }catch(Exception $e){
                $pdo->rollBack();
                //$executeReturn="Failed: ".$e->getMessage();
            }
            //$statement->rowCount();
            return $executeReturn;
        }
    }
?>